<?php
namespace app\staff\controller;

use think\Request;
use think\Db;
use think\Session;
use think\Controller;
use think\Config;
// use Godok\Org\Auth;
use think\Log;

class Login extends Controller
{

	// 登录  
	function login(){
		$param = Request()->param();
		$user = $param['username'];
		$paw = md5($param['password']);
		$res = Db::table('sdzx_employee')->alias('e')
		->field('eg.code,e.id')
		->join('sdzx_employee_group_relation egr','egr.uid=e.id')
		->join('sdzx_employee_group eg','eg.id=egr.group_id')
		->where(['e.username'=>$user,'e.password'=>$paw])
		->find();

		$re = Db::name('employee')->where('id',$res['id'])->setField('client_id',$param['cid']);
		if($re===false){
			$this->error('设备id更新失败');
		}
		if($res){
			$this->success('登陆成功','',$res);
		}else{
			$this->error('登陆失败');
		}
		// $id = $res['id'];
		// if($res['code']=='GJ'){				//管家登陆
		// 	$list = Db::table('sdzx_shop')->alias('s')
		// 	->field('s.shop_owner,s.shop_name,u.username,u.id')
		// 	->join('sdzx_employee e','s.agent_id=e.id','left')
		// 	->join('sdzx_user u','s.user_id=u.id')
		// 	->order('s.create_time asc')
		// 	->where("e.id=$id")
		// 	->select();
		// 	$list['id'] = $id;
		// 	$this->success('登陆成功','',$list);
		// }else if($res['code']=='CG'){		//采购员登陆
		// 	$purchase = Db::table('sdzx_purchase')->alias('p')
		// 	->join('sdzx_purchase_goods pg','p.purchase_id = pg.purchase_id')
		// 	->field('COUNT(pg.pg_id) as num,SUM(pg.num*pg.price) as sum,p.create_time,p.purchase_id')
		// 	->group('p.create_time')
		// 	->where(['p.employee_id'=>$id,'p.status'=>0,'pg.status'=>0])
		// 	->order('p.create_time desc')
		// 	->select();
		// 	foreach ($purchase as $key => $value) {
		// 		$list = Db::name('purchase_goods')->where(['purchase_id'=>$value['purchase_id'],'status'=>0])->select();
		// 		$finish = 1;
		// 		/* 判断是否采购完 1--已采购完 0--未采购完 */
		// 		foreach ($list as $k => $val) {
		// 			if($val['num']!=$val['real_num']){
		// 				$finish = 0;	
		// 			}
		// 		}
		// 		$purchase[$key]['finish'] = $finish;
		// 	}
		// 	array_multisort(array_column($purchase,'finish'), SORT_ASC,array_column($purchase,'create_time'),SORT_ASC,$purchase);
		// 	$purchase['id'] = $id;
		// 	$this->success('登陆成功cg','',$purchase);
		// }else if($res['code']=='PS'){		//配送员登陆
		// 	$res = Db::table('sdzx_shipping')
		// 	->alias('s')
		// 	->join('sdzx_order o','s.order_id=o.order_id')
		// 	->join('sdzx_shop sp','o.user_id=sp.user_id','left')
		// 	->field('s.*,o.receiver_name,o.receiver_mobile,o.receiver_address,o.receiver_pcd,DATE(o.pay_time) as time,o.order_no,o.remarks,sp.shop_name')
		// 	->where("find_in_set($id,s.delivery_id) and order_status=40")
		// 	->select();
		// 	$arr = array();

		// 	foreach ($res as $key => $value) {
		// 		$value['list'] = Db::name('order_goods')->where('order_id',$value['order_id'])->select();
		// 		$value['sum'] = Db::table('sdzx_order_goods')->alias('og')->field('SUM(og.num) as sum')->where('order_id',$value['order_id'])->find();
		// 		$arr[] = $value;
		// 	}
		// 	$arr['id'] = $id;
		// 	$this->success('登陆成功ps','',$arr);
		// }else{
		// 	$this->error('登陆失败');
		// }
		// $this->success('登陆成功','',$res);
	}

    /**
     * 退出
     */
    // http://127.0.0.1/auth/Login/logout
    public function logout()
    {
    	Auth::clean();
    	$this->success('退出成功、前往登录页面');
    }


    /* 用户注册 */
    public function register()
    {
    	
    }

}

